import {create} from 'zustand'
import {Session} from '#/entity'
import {IGetCommon} from '@/api/methods/common.type'

interface IToken {
  // 令牌
  token: string
  setToken: (token: string) => void
  // 开放ID
  openId: string
  setOpenId: (openId: string) => void
  // 用户信息
  userInfo: Session | {}
  setUserInfo: (userInfo: Session) => void
  // 权限列表
  permissionList: IGetCommon.ResPermissionList[]
  setPermissionList: (permissionList: IGetCommon.ResPermissionList[]) => void
}

const initialUserInfo = {
  // 令牌
  token: '',
  // 开放ID
  openId: '',
  // 用户信息
  userInfo: {},
  // 权限列表
  permissionList: []
}

export const useTokenStore = create<IToken>()((set) => ({
  ...initialUserInfo,
  // 设置令牌
  setToken: (token: string) => set({token}),
  // 设置开放ID
  setOpenId: (openId: string) => set({openId}),
  // 设置用户信息
  setUserInfo: (userInfo: Session) => set({userInfo}),
  // 设置权限列表
  setPermissionList: (permissionList: IGetCommon.ResPermissionList[]) => set({permissionList})
}))
